<template>
  <template v-if="opts.length">
    <template v-if="opts.length > 1">
      <el-dropdown trigger="click" placement="bottom-start" @command="handleAudit">
        <el-button v-bind="$attrs">
          审 核<svg t="1731051363182" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7443" width="20" height="20"><path d="M302.016 367.232a32 32 0 0 0-6.976 40.32l3.328 4.736 214.848 252.16 212.48-252.288a32 32 0 0 0-44.8-45.312l-4.096 4.096-163.904 194.56-165.76-194.688a32 32 0 0 0-40.448-6.976l-4.672 3.392z" p-id="7444" fill="currentColor"></path></svg>
        </el-button>
        <template #dropdown>
          <el-dropdown-menu v-matomo>
            <el-dropdown-item v-for="(d, i) in opts" :key="i" :command="d.value">{{ d.label
            }}</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </template>
    <el-button v-if="opts.length === 1" v-bind="$attrs" @click="handleAudit(opts[0].value)">{{ opts[0].label }}</el-button>
    <el-dialog v-if="load" :close-on-click-modal="false" :title="item.label" v-model="visible" append-to-body align-center
      width="500px" @close="handleClose">
      <el-form style="min-height: 170px;">
        <template v-if="isConfirm">
          <el-form-item label="是否变更">
            <el-radio-group v-model="form.audit_result" class="ml-4">
              <el-radio :value="1">可以变更</el-radio>
              <el-radio :value="2">不可以变更</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.audit_remark" type="textarea" :rows="5"></el-input>
          </el-form-item>
        </template>
        <template v-else>
          <el-form-item label="审核">
            <el-radio-group v-model="form.audit_result" class="ml-4">
              <el-radio :value="1">通过</el-radio>
              <el-radio :value="2">驳回</el-radio>
            </el-radio-group>
          </el-form-item>
          <template v-if="form.audit_result === 2">
            <slot v-bind="{form}">
              <el-form-item label="驳回原因">
                <el-input v-model="form.audit_remark" type="textarea" :rows="5"></el-input>
              </el-form-item>
            </slot>
          </template>
        </template>
      </el-form>
      <template #footer>
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" :loading="loadings.submit" @click="submit">确 定</el-button>
      </template>
    </el-dialog>
  </template>
</template>

<script setup lang="jsx">
import api from './api'
import { ElMessage, ElMessageBox } from 'element-plus'
import { $auth, copy, yesno } from '@/utils'
import { resMessage } from '@/utils/res'
const props = defineProps({
  url: { type: String, default: '' },
  field: { type: String, required: true },
  rows: { type: Array, default: () => ([]) },
  list: { type: Array, default: () => ([{ label: '批量审核', value: 1 }]) },
  confirms: { type: Array, default: () => ([]) },
  tableRef: { type: Object, default: () => ({}) },
  auth: { type: [String, Array], default: () => ([]) },
  statusField: { type: String, default: '' },
  onlyone: Boolean,
})

const opts = computed(() => {
  const { list, auth } = props
  if (!auth) return list
  if (typeof auth === 'string') {
    return $auth(auth) ? list : []
  }
  if (!auth.length) return list
  return list.filter((d, i) => auth[i] && $auth(auth[i]))
})

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const emit = defineEmits(['close', 'success'])

const form = ref({})
const selection = ref()
const success = ref(false)
const item = ref()
const isConfirm = computed(() => props.confirms.includes(item.value.value))

const handleAudit = (val) => {
  const selection = props.rows?.length ? props.rows : props.tableRef.$table.getCheckboxRecords()
  open(selection, props.list?.find(d => d.value === val))
}

// 打开弹窗
const message = ref({ yes: [], no: [] })
const open = async (checkeds, option = { label: '批量审核', value: 1 }) => {
  if (checkeds && !checkeds.length) {
    ElMessage.warning('请先选择行!')
    return
  }
  selection.value = checkeds
  message.value = { yes: [], no: [] }
  const { statusField } = props
  if (statusField) {
    const { value } = option
    const { yes, no } = yesno(checkeds, (d) => d[statusField] === value)
    if (!yes) return
    message.value = { yes, no }
    selection.value = yes
  }
  success.value = false
  item.value = option || { label: 'title', value: 1 }
  if (!load.value) {
    load.value = true
    await 1
  }
  form.value = { audit_result: 1, audit_remark: '' }
  loadings = reactive({})
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
  success.value && emit('success')
}
// 提交
const submit = async () => {
  loadings.submit = true
  const { url, field } = props
  if (props.onlyone) {
    form.value[field] = selection.value[0][field]
  } else {
    form.value[field + 's'] = selection.value.map(d => d[field])
  }
  form.value.audit_status = item.value.value
  api.submit(url, form.value).then(res => {
    resMessage({
      res,
      btnText: '复制不符合条件信息',
      success: () => {
        ElMessage.success('操作成功')
        success.value = true
        close()
      },
      error: close,
      danger: () => {
        success.value = true
        close()
      }
    }).then(res => {
      close()
      copy(res.fail_message)
    })
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>